#include <stdio.h>
#include <stdlib.h>

int getcount(char ch[], int n)
{
    int i = 0;
    int j = 0;
    int count = 0;

    for(i = 0; i < n; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if(ch[i] > ch[j])
                count++;
        }
    }
    return count;
}


int main()
{
    char** p1 = NULL;
    int**   p2 = NULL;

    int row = 0, col = 0;
    int i = 0, j = 0, temp1 = 0, temp2 = 0;

    scanf("%d%d", &col, &row);

    p1 = (char**)malloc(row * sizeof(char*));
    p2 = (int**)malloc(row * sizeof(int*));
    for(i = 0; i < row; i++)
    {
        p1[i] = (char*)malloc(sizeof(char) * (col + 1));
        p2[i] = (int*)malloc(sizeof(int) * 2);
        scanf("%s", p1[i]);
        p2[i][0] = getcount(p1[i], col);
        p2[i][1] = i;
    }

    for(i = 0; i < row; i++)
    {
        for(j = i; j < row; j++)
        {
            if(p2[i][0] > p2[j][0])
            {
                temp1 = p2[i][0];
                temp2 = p2[i][1];
                p2[i][0] = p2[j][0];
                p2[i][1] = p2[j][1];
                p2[j][0] = temp1;
                p2[j][1] = temp2;
            }
        }
    }


    for(i = 0; i < row; i++)
    {
        printf("%s\r\n", p1[p2[i][1]]);
    }

    return 0;
}
